unit unit_eQSLutils;

{$mode objfpc}{$H+}

interface
uses
  Classes, SysUtils, HTTPSend, Dialogs;
function eQSLLogin(fUser, fPassword: String): String;
function eQSLGenerateInbox: String;
function eQSLDownload(URL, TargetFile: String): String;
function eQSLUseJPG:Boolean;
function eQSLGetQSL(URL,TargetFile:String):Boolean;
function eQSLCountInbox(InboxFile:String):Integer;
implementation
var MyCookie:String;
  HTTP:THTTPSend;
  UseJPG:Boolean;
function eQSLLogin(fUser, fPassword: String): string;

var
  URLLogin : String;
  i:Integer;
  LoginOK:Boolean;
begin
  URLLogin :=  'http://www.eqsl.cc/qslcard/LoginFinish.cfm?Callsign=' + fUser + '&EnteredPassword=' + fPassword + '&Login=Go';
  UseJPG:=false;
  LoginOK:=false;
  HTTP:=THTTPSend.Create;

try
  if HTTP.HTTPMethod('GET',URLLogin) then
     begin
       if (HTTP.ResultCode = 200) and (HTTP.Cookies.Text <> '')then
         begin
                         for i:=0 to HTTP.Cookies.Count-1 do
                         begin
                              if (HTTP.Cookies[i] = 'LOGINCALLSIGN=' + UpperCase(fUser)) Then

                              LoginOK:=true;
                                            if HTTP.Cookies[i] = 'USEJPG=1' then
                                            UseJPG:=true;

                         end;
           MyCookie:=HTTP.Cookies.Text;
           //ShowMessage(MyCookie);
           result:='';
         end
       else
         begin
           {Otra respuesta distinta a 200 es un problema del server}
           result:=IntToStr(HTTP.ResultCode);
         end;
     end
  else
  result:=IntToStr(HTTP.ResultCode);
  //ShowMessage('Durante la ejecución de la consulta se produjo el error!')
finally
HTTP.Free;
end;
end;
function eQSLGenerateInbox: String;
var
  URLInbox:String;
  page:TStringList;
  i: integer;
  position : Integer;
  p:String;
  tTemp: TStringList;
  begin
  URLInbox:='http://www.eqsl.cc/qslcard/DownloadInBox.cfm';
  p:='';
  page:=TStringList.Create;
    HTTP:=THTTPSend.Create;
           HTTP.Clear;
           HTTP.Cookies.Text:=MyCookie;
           try
           if HTTP.HTTPMethod('GET', URLInbox) then
             begin
                  page.LoadFromStream(HTTP.Document);
                         for i:=0 to page.Count-1 do
                         begin
                         position := AnsiPos('.TXT file', page[i]);
                         //ShowMessage(page[i]);
                                  if position <> 0 then
                                       begin
                                         p:=page[i];
                                         p:=trim(p);
                                         p:=StringReplace(p, '<LI><A HREF="', '', [rfReplaceAll, rfIgnoreCase]);
                                         p:=StringReplace(p, '">.TXT file</A>', '', [rfReplaceAll, rfIgnoreCase]);
                                         p:='http://www.eqsl.cc/qslcard/' + p;
                                         //ShowMessage(P);
                                         result:=p;
                                     end;
           end;
           end;
           finally
             HTTP.Free;
             page.Free;
           end;
  end;
function eQSLDownload(URL, TargetFile: String): String;
{FALTA DEVOLVER ALGUN FLAG INDICANDO QUE PUDO O NO BAJAR EL ARCHIVO}
     begin
          HTTP:=THTTPSend.Create;
          HTTP.Clear;
          HTTP.Cookies.Text:=MyCookie;
          try
          if HTTP.HTTPMethod('GET', URL) then
            begin
              with TFileStream.Create(TargetFile,fmCreate or fmOpenWrite) do
                          try
                            Seek(0, soFromBeginning);
                            CopyFrom(HTTP.Document, 0);
                            result:='';
                          finally
                            Free;
                          end;

            end
            else
           result:='NoOK';

          finally
            HTTP.Free;
          end;
     end;
function eQSLCountInbox(InboxFile:String):Integer;
var
myFile:TextFile;
myLine:String;
Count:Integer;
begin
     Count:=0;
     AssignFile(myFile, InboxFile);
     FileMode := fmOpenRead;
     try
     Reset(myFile);
     while not Eof(myFile) do
     begin
       Readln(myFile, myLine);
       if AnsiPos('<CALL:', myLine) = 1 then
       Count:=Count+1;
     end;
     finally
     CloseFile(myFile);
     result:=Count;
     end;

end;

function eQSLUseJPG:Boolean;
begin
result:=UseJPG;
end;
function eQSLGetQSL(URL,TargetFile:String):Boolean;
var
  page:TStringList;
  i: integer;
  position:integer;
  p:String;
  x:String;
  HTTP:THTTPSend;
begin
     page:=TStringList.Create;
     HTTP:=THTTPSend.Create;
     HTTP.Clear;
     HTTP.Cookies.Text:=MyCookie;
         try
         if HTTP.HTTPMethod('GET', URL) then
           begin
            page.LoadFromStream(HTTP.Document);

                         for i:=0 to page.Count-1 do
                         begin
                         position:= AnsiPos('<img src="', page[i]);
                         if position <> 0 then
                            begin
                         p:=page[i];
                         p:=trim(p);
                         p:=StringReplace(p, '" alt="" />', '', [rfReplaceAll, rfIgnoreCase]);
                         p:=StringReplace(p, '<img src="', '', [rfReplaceAll, rfIgnoreCase]);
                         p:='http://www.eqsl.cc' + p;
                         x:=eQSLDownload(p, TargetFile);
                         result:=True;
                            end;
                         end;
            end;


           finally
             HTTP.Free;
             page.Free;
           end;
end;

end.

